#include "StdAfx.h"
#include ".\soundmaker.h"


#include "mmreg.h"

#include "DSound.h"


class SoundMakerImpl
{

	HRESULT m_hrCreated;
	LPDIRECTSOUND m_pDSound;

	bool m_bFailed;
public:
	SoundMakerImpl();
	~SoundMakerImpl();

	bool AmIValid()
	{
		return !m_bFailed && (m_pDSound != NULL);
	}
};

SoundMakerImpl::SoundMakerImpl()
{

	m_pDSound = NULL;
	HRESULT hr =
		DirectSoundCreate(NULL, &m_pDSound, NULL);
	m_bFailed = FAILED(hr);

}

SoundMakerImpl::~SoundMakerImpl()
{

}

static SoundMakerImpl * CreateSoundMakerImpl()
{
	SoundMakerImpl * pResult = new SoundMakerImpl;
	if(pResult->AmIValid())
	{
		return pResult;
	}
	delete pResult;
	return NULL;
}

static SoundMakerImpl * g_pImpl = NULL;
static int g_nSoundImplRefCount = 0;

SoundMaker::SoundMaker():m_bValid(false)
{
}

SoundMaker::~SoundMaker()
{
	if(m_bValid)
	{
		ReleaseImpl();
	}
}

void SoundMaker::CreateImpl(void)
{
	if(m_bValid)
	{
		return;
	}
	if(!g_pImpl)
	{
		g_pImpl = CreateSoundMakerImpl();
	}
	m_pImpl = g_pImpl;
	if(m_pImpl)
	{
		++g_nSoundImplRefCount;
		m_bValid = true;
	}
	
}

void SoundMaker::ReleaseImpl(void)
{
	if(!m_bValid)
	{
		return;
	}
	--g_nSoundImplRefCount;
	if(g_nSoundImplRefCount < 1 && g_pImpl != NULL)
	{
		delete g_pImpl;
		g_pImpl = NULL;
		g_nSoundImplRefCount = 0;
	}
	m_bValid = false;
}

void SoundMaker::Test()
{
}

void SoundMaker::TestFname(const CString & strFname)
{

}
